package org.jin.jinaicode.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.jin.jinaicode.common.BaseResponse;
import org.jin.jinaicode.common.ResultUtils;
import org.jin.jinaicode.exception.BusinessException;
import org.jin.jinaicode.exception.ErrorCode;
import org.jin.jinaicode.exception.ThrowUtils;
import org.jin.jinaicode.model.dto.user.UserLoginDTO;
import org.jin.jinaicode.model.dto.user.UserRegisterDTO;
import org.jin.jinaicode.model.dto.user.UserUpdateDTO;
import org.jin.jinaicode.model.vo.UserLoginVO;
import org.jin.jinaicode.model.vo.UserRegisterVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.jin.jinaicode.model.entity.User;
import org.jin.jinaicode.service.UserService;
import org.springframework.web.bind.annotation.RestController;

import static org.jin.jinaicode.constant.UserConstant.USER_LOGIN_STATE;

/**
 * 用户 控制层。
 *
 * @author 小堇
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public BaseResponse<UserRegisterVO> register(@RequestBody UserRegisterDTO userRegisterDTO) {
        if (userRegisterDTO == null) {
            ThrowUtils.throwIf(true, ErrorCode.valueOf("参数不能为空"));
        }
        String userAccount = userRegisterDTO.getUserAccount();
        String email = userRegisterDTO.getEmail();
        String captcha = userRegisterDTO.getCaptcha();
        String userPassword = userRegisterDTO.getUserPassword();
        String checkPassword = userRegisterDTO.getCheckPassword();
        String inviteUtm = userRegisterDTO.getInviteUtm();
        if (StringUtils.isAllBlank(userAccount, userPassword, checkPassword, email, captcha)) {
            ThrowUtils.throwIf(true, ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        return userService.userRegister(userAccount, email, captcha, userPassword, checkPassword, inviteUtm);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public BaseResponse<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        if (userLoginDTO == null) {
            ThrowUtils.throwIf(true, ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        String userAccount = userLoginDTO.getUserAccount();
        String userPassword = userLoginDTO.getUserPassword();
        String email = userLoginDTO.getEmail();
        if (StringUtils.isAllBlank(userAccount, userPassword)) {
            ThrowUtils.throwIf(true, ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        return userService.userLogin(userAccount, email, userPassword, request);
    }

    /**
     * 获取当前登录用户
     */
    @GetMapping("/getLoginUser")
    public BaseResponse<UserLoginVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(userService.getLoginUserVO(loginUser));
    }

    /**
     * 用户退出
     */
    @PostMapping("/logout")
    public BaseResponse<Object> logout(HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
        User currentUser = (User) attribute;
        if (currentUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "用户未登录");
        }
        request.getSession().removeAttribute(USER_LOGIN_STATE);
        return ResultUtils.success(ErrorCode.SUCCESS);
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/update")
    public BaseResponse<UserLoginVO> updateUserInfo(@RequestBody UserUpdateDTO userUpdateDTO,
            HttpServletRequest request) {
        if (userUpdateDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        return userService.updateUserInfo(userUpdateDTO, request);
    }
}
